package ui;
import javax.imageio.ImageIO;
import javax.swing.*;

import com.mysql.jdbc.PreparedStatement;
import common.Author;

import database.AuthorManager;
import database.DBManager;

import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;



public class AddAuthorUI extends JFrame {
	



		private JTextField textAuthorName;
		private  JTextField textAuthorLastName;
		private JTextField textFieldAuthorId;
		
		
		public AddAuthorUI() {
			
		       Image image = null;
			try {
				image = ImageIO.read(new File("images\\body.jpg"));
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
				this.setContentPane(new ImagePanel(image));
			
			addWindowListener(new WindowAdapter() {
				@Override
				public void windowClosing(WindowEvent e) {
					
					setVisible(false);
					MainMenuUI.X.initializeBooklist();
					MainMenuUI.X.setVisible(true);
					
				}
			});
			this.setDefaultCloseOperation(HIDE_ON_CLOSE);
			setBounds(new Rectangle(300, 200, 440, 300));
			setResizable(false);
			getContentPane().setLayout(null);
			
			JLabel lblAuthorName = new JLabel("Author Name:");
			lblAuthorName.setFont(new Font("MV Boli", Font.BOLD, 12));
			lblAuthorName.setForeground(new Color(139, 69, 19));
			lblAuthorName.setBounds(26, 63, 105, 14);
			getContentPane().add(lblAuthorName);
			
			JLabel lblAuthorLastName = new JLabel("Author Last Name:");
			lblAuthorLastName.setFont(new Font("MV Boli", Font.BOLD, 12));
			lblAuthorLastName.setForeground(new Color(139, 69, 19));
			lblAuthorLastName.setBounds(26, 104, 127, 17);
			getContentPane().add(lblAuthorLastName);
			
			textAuthorName = new JTextField();
			textAuthorName.setBounds(163, 61, 215, 17);
			getContentPane().add(textAuthorName);
			textAuthorName.setColumns(10);
			
			textAuthorLastName = new JTextField();
			textAuthorLastName.setBounds(163, 102, 215, 20);
			getContentPane().add(textAuthorLastName);
			textAuthorLastName.setColumns(10);
			
			JLabel lblAuthorId = new JLabel("Author id:");
			lblAuthorId.setFont(new Font("MV Boli", Font.BOLD, 12));
			lblAuthorId.setForeground(new Color(139, 69, 19));
			lblAuthorId.setBounds(26, 144, 127, 17);
			getContentPane().add(lblAuthorId);
			
			textFieldAuthorId = new JTextField();
			textFieldAuthorId.setColumns(10);
			textFieldAuthorId.setBounds(163, 142, 215, 20);
			getContentPane().add(textFieldAuthorId);
			
			
			JLabel lblAddBook = new JLabel("ADD AUTHOR");
			lblAddBook.setFont(new Font("MV Boli", Font.BOLD, 22));
			lblAddBook.setForeground(new Color(139, 69, 19));
			lblAddBook.setBounds(120, 11, 168, 20);
			getContentPane().add(lblAddBook);
			
			
           ImageIcon b3 = new ImageIcon("images\\add.png");
			
			JButton btnAdd = new JButton(b3);
			btnAdd.setBorder(BorderFactory.createEmptyBorder());
			
	           btnAdd.addMouseListener(new MouseAdapter() {
					  @Override
					  public void mouseClicked(MouseEvent e) {
			
					//AddAuthor();
					Author a;
					a=new Author();
					
					if(check()==true){
						a.setId((int)Integer.parseInt(textFieldAuthorId.getText()));
						a.setName(textAuthorName.getText());
						a.setLastName(textAuthorLastName.getText());
					if(AuthorManager.addAuthor(a)){
						textAuthorName.setText("");
						textAuthorLastName.setText("");
						textFieldAuthorId.setText("");
						
					
					MainMenuUI.X.getComboBox().addItem(a.getName()+" "+a.getLastName());
					MainMenuUI.X.getAuthoridarry().add(a.getId());
					infoBox("INFORMATION: Author Addition Sucseeded! ", "TITLE BAR MESSAGE");
					
			
					    }
					}
					else{
						infoBox("Warning: Author Addition Failed! ", "TITLE BAR MESSAGE");
						
					}
					
				}
			});
			btnAdd.setBounds(336, 226, 68, 23);
			getContentPane().add(btnAdd);
			
			ImageIcon b1 = new ImageIcon("images\\back.png");
			
			
			JButton btnBack = new JButton(b1);
			////
			btnBack.setBorder(BorderFactory.createEmptyBorder());
			
			btnBack.addMouseListener(new MouseAdapter() {
				  @Override
				  public void mouseClicked(MouseEvent e) {
			
					setVisible(false);
					MainMenuUI.X.initializeBooklist();
					MainMenuUI.X.setVisible(true);
				}
			});
			btnBack.setBounds(26, 226, 68, 23);
			getContentPane().add(btnBack);
			
			

			ImageIcon b2 = new ImageIcon("images\\reset.png");
		
			
			JButton btnReset = new JButton(b2);
btnReset.setBorder(BorderFactory.createEmptyBorder());
			
			btnReset.addMouseListener(new MouseAdapter() {
				  @Override
				  public void mouseClicked(MouseEvent e) {
				       textAuthorName.setText("");
					textAuthorLastName.setText("");
					textFieldAuthorId.setText("");
				}
			});
			btnReset.setBounds(177, 226, 68, 23);
			getContentPane().add(btnReset);
			
		
			this.setVisible(true);
	}

public void infoBox(String infoMessage, String location)
{
    JOptionPane.showMessageDialog(null, infoMessage, "InfoBox: " + location, JOptionPane.INFORMATION_MESSAGE);
}
public  JTextField getTextAuthorName() {
return textAuthorName;
}

public void setTextAuthorName(JTextField textAuthorName) {
this.textAuthorName = textAuthorName;
}

public  JTextField getTextAuthorLastName() {
return textAuthorLastName;
}

public void setTextAuthorLastName(JTextField textAuthorLastName) {
this.textAuthorLastName = textAuthorLastName;
}

public  JTextField getTextFieldAuthorId() {
return textFieldAuthorId;
}

public void setTextFieldAuthorId(JTextField textFieldAuthorId) {
this.textFieldAuthorId = textFieldAuthorId;
}




public boolean check(){

	//if id is empty
	if(getTextFieldAuthorId().getText().equals("")){
		
		infoBox("Warning: You have to insert numerical author id! ", "TITLE BAR MESSAGE");
		return false;
	}
	//if id already exists
	ArrayList<Author> arr =new ArrayList<Author>();
	arr=AuthorManager.getAllAuthors();
	for(int i=0;i<arr.size();i++){
	if((arr.get(i).getId())==Integer.parseInt(textFieldAuthorId.getText())){
		infoBox("Warning: Author id already exists! ", "TITLE BAR MESSAGE");
		return false;
	}
	}
	//if author name is empty
	if(getTextAuthorName().getText().equals("")){
		infoBox("Warning: You have to insert author name! ", "TITLE BAR MESSAGE");
		return false;
	}
	//is author last name is is empty
	if(getTextAuthorLastName().getText().equals("")){
		infoBox("Warning: You have to insert author last name! ", "TITLE BAR MESSAGE");
		return false;}
	
	return true;
 }
}
